<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class penanganan_segmen extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->load->library('flexigrid');
        $this->load->library('form_validation');
		$this->load->library('session');
        $this->load->helper('flexigrid');
        $this->load->helper(array('form', 'url'));
        $this->load->model('penanganan_model');
        $this->load->model('penanganan_segmen_model');
        $this->load->model('jalan_model');
        $this->load->model('keping_model');
        $this->load->model('keping_segmen_model');
    }

    //load Grid
    function index($id_keping) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $colModel['no'] = array('No', 30, TRUE, 'center', 0);
			$colModel['TAHUN'] = array('Tahun', 120, TRUE, 'center', 1);
            $colModel['PENANGANAN'] = array('Penanganan', 150, TRUE, 'center', 1);
            $colModel['ALOKASI'] = array('Alokasi Penanganan', 100, TRUE, 'center', 1);
            $colModel['Edit'] = array('Perbarui', 70, TRUE, 'center', 1);

            // get nama keping jalan
			// $hasil = $this->keping_model->selectone($id_keping);
			// foreach ($hasil->result() as $row) {
                // $nama = $row->KEPING;
            // }
			$this->session->set_userdata('id_keping', $id_keping);
			// $this->session->set_userdata('nama_ruas_jalan', $nama_ruas_jalan);

            $gridParams = array(
                'width' => 'auto',
                'height' => 300,
                'rp' => 15,
                'rpOptions' => '[5,10,15,20,25,40]',
                'pagestat' => 'Menampilkan: {from} hingga {to} dari {total} data.',
                'blockOpacity' => 0.5,
                'title' => 'Daftar Penanganan Keping Jalan',
                'showTableToggleBtn' => true
            );

            $buttons[] = array('Tambah', 'add', 'spt_js');
            $buttons[] = array('separator');
			$buttons[] = array('Hapus', 'delete', 'spt_js');
			$buttons[] = array('separator');
            $buttons[] = array('Kembali ke daftar keping jalan', 'back', 'spt_js');


            // mengambil data dari file controler ajax pada method grid_berkas
            $grid_js = build_grid_js('flex1', site_url("penanganan_segmen/grid/".$id_keping.""), $colModel, 'TAHUN', 'asc', $gridParams, $buttons);

            $data['added_js'] =
                    "<script type='text/javascript'>
        function spt_js(com,grid)
        {
                if (com=='Tambah')
                {
                        location.href='" . base_url() . "index.php/penanganan_segmen/form';
                }
                
                if (com=='Kembali ke daftar keping jalan')
                {
                        location.href='" . base_url() . "index.php/keping_segmen/index/".$this->session->userdata('no_ruas')."';
                }

                if (com=='Hapus')
                        {
                           if($('.trSelected',grid).length>0){
                                   if(confirm('Anda yakin menghapus ' + $('.trSelected',grid).length + ' penanganan jalan?')){
                                                var items = $('.trSelected',grid);
                                                var itemlist ='';
                                                for(i=0;i<items.length;i++){
                                                        itemlist+= items[i].id.substr(3)+',';
                                                }
                                                $.ajax({
                                                   type: 'POST',
                                                   url: '" . base_url() . "index.php/penanganan/delete" . "',
                                                   data: 'items='+itemlist,
                                                   success: function(data){
                                                        $('#flex1').flexReload();
                                                   }
                                                });
                                        }
                                } else {
                                        return false;
                                }
                        }

        }
        </script>
        ";


            $data['js_grid'] = $grid_js;
            $data['content'] = $this->load->view('grid', $data, true);
            $this->load->view('main', $data);
        }
    }

    function grid($id_keping) {
        $valid_fields = array('TAHUN', 'PENANGANAN');
        $this->flexigrid->validate_post('TAHUN', 'asc', $valid_fields);
        $records = $this->penanganan_segmen_model->get_data($id_keping);
        $this->output->set_header($this->config->item('json_header'));
        $no = 0;

        foreach ($records['records']->result() as $row) {
            $record_items[] = array(
                $row->ID_PENANGANAN,
                $no = $no + 1,
                $row->TAHUN,
				$row->PENANGANAN,
				$row->ALOKASI,
                '<a href=\'' . base_url() . 'index.php/penanganan_segmen/edit/' . $row->ID_PENANGANAN. '\'><img border=\'0\' src=\'' . base_url() . 'images/grid/edit.png\'></a> ',
            );
        }

        if (isset($record_items))
            $this->output->set_output($this->flexigrid->json_build($records['record_count'], $record_items));
        else
            $this->output->set_output('{"page":"1","total":"0","rows":[]}');
    }
    
    
    
    function form() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data['content'] = $this->load->view('form_penanganan_segmen', $data, true);
            $this->load->view('main', $data);
        }
    }
	
	
	
    
	
	function add() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() != false)
			{
				$this->form();
			}
			else{
				$data = array(
					'TAHUN' => $this->input->post('tahun'),
					'PENANGANAN' => $this->input->post('penanganan'),
					'ALOKASI' => $this->input->post('alokasi'),
					'ID_KEPING_SEGMEN' => $this->session->userdata('id_keping')
				);
				$this->penanganan_segmen_model->insert($data);
				redirect('penanganan_segmen/index/'.$this->session->userdata('id_keping').'');
			}
        }
    }
	
	
	
	function form_masal() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'new';
            $data['failed'] = false;
            $data['aa'] = '';
			$data['content'] = $this->load->view('form_penanganan_segmen_masal', $data, true);
            $this->load->view('main', $data);
        }
    }
	
	
	
	function add_masal() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() != false)
			{
				$this->form();
			}
			else{
				//get id_keping
				$id_periode = $this->session->userdata('id_periode');
				$no_ruas = $this->session->userdata('no_ruas');
				$record_artikel = $this->keping_segmen_model->get_data_keping($id_periode, $no_ruas);
				foreach ($record_artikel->result() as $artikel){
					$id_keping = $artikel->ID_KEPING_SEGMEN;
					$data = array(
						'TAHUN' => $this->input->post('tahun'),
						'PENANGANAN' => $this->input->post('penanganan'),
						'ALOKASI' => $this->input->post('alokasi'),
						'ID_KEPING_SEGMEN' => $id_keping
					);
					$this->penanganan_segmen_model->insert($data);
				}
				
				redirect('keping_segmen/index/'.$this->session->userdata('no_ruas').'');
			}
        }
    }
	
	

    public function edit($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $data['status'] = 'edit';
            $record_artikel = $this->penanganan_segmen_model->selectone($id);
            foreach ($record_artikel->result() as $artikel){
                $data['id_penanganan'] = $artikel->ID_PENANGANAN;
                $data['tahun'] = $artikel->TAHUN;
                $data['penanganan'] = $artikel->PENANGANAN;
                $data['alokasi'] = $artikel->ALOKASI;
                $data['id_keping'] = $artikel->ID_KEPING_SEGMEN;
            }
            $data['failed'] = false;
            $data['aa'] = '';
            $data['content'] = $this->load->view('form_penanganan_segmen', $data, true);
            $this->load->view('main', $data);
        }
    }

    function update($id) {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
			if($this->cek_validasi() != false)
				{
					$this->edit($id);
				}
				else{
					$data = array(
						'TAHUN' => $this->input->post('tahun'),
						'PENANGANAN' => $this->input->post('penanganan'),
						'ALOKASI' => $this->input->post('alokasi')
					);
					$this->penanganan_segmen_model->update($id, $data);
					redirect('penanganan_segmen/index/'.$this->session->userdata('id_keping').'');
				}
        }
    }

    
    function delete() {
        if ($this->session->userdata('username') == NULL) {
            redirect('login');
        } else {
            $spt_ids_post_array = explode(",", $this->input->post('items'));
            foreach ($spt_ids_post_array as $index => $id) {
                if (isset($id) && $id != '') {
                    $this->penanganan_model->delete($id);
                }//end if
            }//end foreach
        }
    }
	
	
	function cetak_keping(){
		//get data jalan
		$no_ruas = $this->session->userdata('no_ruas');
		$data['data_keping'] = $this->keping_model->get_data_keping($no_ruas);
		$data['no_ruas'] = $no_ruas;
		$data['nama_ruas_jalan'] = $this->session->userdata('nama_ruas_jalan');
		$hasil = $this->jalan_model->selectone($no_ruas);
		foreach ($hasil->result() as $row) {
			$data['panjang_jalan'] = $row->PANJANG;
			$data['gps_awal_lat'] = $row->GPS_AWAL_LAT;
			$data['gps_awal_long'] = $row->GPS_AWAL_LONG;
			$data['gps_akhir_lat'] = $row->GPS_AKHIR_LAT;
			$data['gps_akhir_long'] = $row->GPS_AKHIR_LONG;
			
		}
		//end get data jalan
		
		$data['nama_periode'] = $this->session->userdata('nama_periode');
		$data['mulai'] = $this->session->userdata('selesai');
		$data['selesai'] = $this->session->userdata('mulai');
		//$data['nama_kelas'] = $this->session->userdata('nama_kelas');
		
		$this->load->view('cetak_keping', $data);
	}

    function cek_validasi() {
        // Setting Rules
        // $this->form_validation->set_rules('nama_santri', 'Nama Santri', 'required');
        // $this->form_validation->set_rules('munakosah', 'No. Induk', 'required');

        // //Setting Error Message
        // $this->form_validation->set_message('required', 'Field %s harus diisi.');

        // // Setting Delimiter
        // $this->form_validation->set_error_delimiters('<li class="error">', '</li>');
        return $this->form_validation->run();
    }
	
	
	
	//test
	function readExcel()
	{
        $this->load->library('csvreader');
        $result =   $this->csvreader->parse_file('tes.csv');

        $data['csvData'] =  $result;
        $this->load->view('view_csv', $data);  
	}

	

}
